/*******************************************************
 * @Title: AgilefastRequestUtil
 * @ProjectName: AgileFastFK3.0
 * @Package: com.caltco.cargo.ops.auth.utils
 * @Description: TODO
 * @author: daixirui
 * @version: V1.0.0
 * @Copyright: 2022 All rights reserved.
 * @date 2022/6/30 15:20</br>
 ********************************************************
 * @date 2022/6/30 15:20
 * @Title: 创建类
 * @version V1.0.0
 * @Description: TODO
/******************************************************/
package com.caltco.cargo.ops.common.utils;

import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;

import java.net.InetAddress;

public class AgilefastRequestUtil {

    /**
     * @return java.lang.String
     * @title 获取当前登录Token
     * @author daixirui
     * @date 2022/7/1 9:55
     */
    public static String getCurrentToken(HttpServletRequest mRequest) {
        String Authorization_ = mRequest.getHeader("Authorization");
        //从header中获取token
        String token = mRequest.getHeader("token");
        //如果header中不存在token，则从参数中获取token
        if (StringUtils.isBlank(token)) {
            token = mRequest.getParameter("token");
        }
        if (Authorization_ == null) {
            Authorization_ = "";
        }
        if (!Authorization_.equals("")) {
            token = Authorization_;
        }
        return token;
    }
    /**
     * @return java.lang.String
     * @title 获取当前客户端Ip地址
     * @author daixirui
     * @date 2022/7/1 9:56
     */
    private String getCurrentRequestIpAddress(HttpServletRequest mRequest) {
        String ip = mRequest.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknow".equalsIgnoreCase(ip)) {
            ip = mRequest.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = mRequest.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = mRequest.getRemoteAddr();
            if (ip.equals("127.0.0.1")) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ip = inet.getHostAddress();
            }
        }
        // 多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ip != null && ip.length() > 15) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        return ip;

    }
}
